package controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import bean.Right;
import bean.User;
import bean.model.PageModel;
import service.RoleService;
import util.PageUtil;

@Controller
public class RoleManagerController {
	RoleService roleService = new RoleService();

	@RequestMapping("/roleManager")
	public String toRoleManager(HttpServletRequest request) {
		if (request.getSession().getAttribute("rolePageModel") == null) {
			PageModel createPage = PageUtil.createPage(15, roleService.getStaffNum(), 1);
			List<User> queryStaffByPage = roleService.queryStaffByPage(createPage);
			request.getSession().setAttribute("rss", queryStaffByPage);
			request.getSession().setAttribute("rolePageModel", createPage);
		}
		return "roleManager";
	}

	@RequestMapping("/roleManagerSearch")
	public ResponseEntity<String> roleManagerSearch(HttpServletRequest request) {
		List<User> list = new ArrayList<User>();
		String userName = request.getParameter("userName");
		String role = request.getParameter("role");
		String rigor = request.getParameter("chk");
		list = roleService.getRoleSearchList(userName, role, rigor);
		String json = JSON.toJSONString(list);
		ResponseEntity<String> responseResult = new ResponseEntity<String>(json, HttpStatus.OK);
		return responseResult;
	}

	@RequestMapping("/roleManagerInformation")
	public ResponseEntity<String> roleManagerInformation(HttpServletRequest request) {
		List<Right> list = new ArrayList<Right>();
		String username = request.getParameter("userNameOfInformation");
		list = roleService.getRightInformation(username);
		String json = JSON.toJSONString(list);
		ResponseEntity<String> responseResult = new ResponseEntity<String>(json, HttpStatus.OK);
		return responseResult;
	}

	@RequestMapping("/roleManagerEdit")
	public ResponseEntity<String> roleManagerEdit(HttpServletRequest request) {
		List<Right> list = new ArrayList<Right>();
		String username = request.getParameter("userRoleUpdate");
		list = roleService.getRightInformation(username);
		String json = JSON.toJSONString(list);
		ResponseEntity<String> responseResult = new ResponseEntity<String>(json, HttpStatus.OK);
		return responseResult;
	}

	@RequestMapping("/roleManagerEditSave")
	public ResponseEntity<JSONObject> roleManagerEditSave(HttpServletRequest request) {
		List<Right> list = new ArrayList<Right>();
		List<String> checkList = new ArrayList<String>();
		boolean result = false;
		String nameUpdate = request.getParameter("nameUpdate");
		String check0 = request.getParameter("check0");
		String check1 = request.getParameter("check1");
		String check2 = request.getParameter("check2");
		String check3 = request.getParameter("check3");
		String check4 = request.getParameter("check4");
		String check5 = request.getParameter("check5");
		String check6 = request.getParameter("check6");
		String check7 = request.getParameter("check7");
		String check8 = request.getParameter("check8");
		String check9 = request.getParameter("check9");
		String check10 = request.getParameter("check10");
		String check11 = request.getParameter("check11");
		String check12 = request.getParameter("check12");
		String check13 = request.getParameter("check13");
		String check14 = request.getParameter("check14");
		checkList.add(check0);
		checkList.add(check1);
		checkList.add(check2);
		checkList.add(check3);
		checkList.add(check4);
		checkList.add(check5);
		checkList.add(check6);
		checkList.add(check7);
		checkList.add(check8);
		checkList.add(check9);
		checkList.add(check10);
		checkList.add(check11);
		checkList.add(check12);
		checkList.add(check13);
		checkList.add(check14);
		result = roleService.roleManagerEdit(nameUpdate, checkList);
		JSONObject json = new JSONObject();
		json.put("result", result);
		ResponseEntity<JSONObject> responseResult = new ResponseEntity<JSONObject>(json, HttpStatus.OK);
		return responseResult;
	}

}
